package com.mmp.main.servlet.joborder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.DB.DBQueryManager;
import com.mmp.main.endpoints.JobOrderNotificationsEndPoint;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.entity.JobOrderSparePart;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class JobOrderTrackingProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String jobOrderId = request.getParameter("jobOrderId");
		String deviceId = request.getParameter("deviceId");

		if (deviceId == null || deviceId.isEmpty() || jobOrderId == null || jobOrderId.isEmpty()) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		JobOrder order = dbm.findById(Integer.parseInt(jobOrderId), JobOrder.class);

		HospitalDevice device = order.getHospitalDevice();

		order.setFirstAction(WebUtil.getParamValue(request, "firstAction", null));
		order.setFirstActionDate(WebUtil.getParamValueAsDate(request, "firstActionDate", null));

		if (order.getJobOrderSpareParts() == null)
			order.setJobOrderSpareParts(new ArrayList<JobOrderSparePart>());
		else
			order.getJobOrderSpareParts().clear();

		order.setFixIncludingSpareParts(("1").equals(request.getParameter("fixIncludingSpareParts")) ? true : false);
		order.setAgentReportUrl(null);

		if (order.getFixIncludingSpareParts()) {

			String[] agentReportUrl = FileUtil.uploadFiles(request, "agentReportUrl");
			if (agentReportUrl != null && agentReportUrl.length != 0)
				order.setAgentReportUrl(Common.concatenateValues(agentReportUrl));

			String jobOrderSpareParts_rowOrder = request.getParameter("jobOrderSpareParts_rowOrder");
			if (jobOrderSpareParts_rowOrder != null && !jobOrderSpareParts_rowOrder.isEmpty()) {
				String[] indexes = jobOrderSpareParts_rowOrder.split(",");
				int quantity = 0;
				float price = 0;
				for (int i = 0; i < indexes.length; i++) {
					JobOrderSparePart sPart = new JobOrderSparePart();
					quantity = WebUtil.getParamValueAsInteger(request, "jobOrderSpareParts_accQuantity_" + indexes[i],
							0);
					price = WebUtil.getParamValueAsFloat(request, "jobOrderSpareParts_accPrice_" + indexes[i], 0);

					sPart.setQuantity(quantity);
					sPart.setPrice(price);
					sPart.setTotalPrice(quantity * price);
					sPart.setDescription(WebUtil.getParamValue(request, "jobOrderSpareParts_accDescription_"
							+ indexes[i], null));
					order.addJobOrderSparePart(sPart);
				}
			}

		}

		order.setSecondAction(WebUtil.getParamValue(request, "secondAction", null));
		order.setSecondActionDate(WebUtil.getParamValueAsDate(request, "secondActionDate", null));

		order.setFinalAction(WebUtil.getParamValue(request, "finalAction", null));
		order.setFinalActionDate(WebUtil.getParamValueAsDate(request, "finalActionDate", null));

		String[] finalReportUrl = FileUtil.uploadFiles(request, "finalReportUrl");
		if (finalReportUrl != null && finalReportUrl.length != 0)
			order.setFinalReportUrl(Common.concatenateValues(finalReportUrl));

		if (!WebUtil.sessionUserRestricted(request) && request.getParameter("closed") != null) {
			order.setClosed(true);
			order.setClosedBy(getSessionUser(request));
			order.setClosedIn(new Date());

			device.setStatus(Enums.DEVICE_STATUS.WORK_PROPERLY.getStatus());
			dbm.mergeEntity(device);
		}

		dbm.mergeEntity(order);

		setMessage(request, new Message("تم الحفظ بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		
		JobOrderNotificationsEndPoint.sendMessageToAll(new DBQueryManager(dbm).getNotification());

		sendRedirect(request, response, "SearchForJobOrderDisplay?" + Default.SEARCH_DEST_PARAM_NAME + "="
				+ Default.SEARCH_DEST_FOLLOWUP_JOB_OREDR_PARAM_VALUE);

	}
}
